package ks.client.controllers.admin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import org.w3c.dom.Document;
import ks.client.UserContext;
import ks.client.ipc.Client;
import ks.client.lobby.Lobby;
import ks.framework.common.Message;


/**
 * 
 * If the "Add New Icon" button is clicked in the icon manager GUI,
 * an icon-add command is sent to the server to add the specified
 * icon.
 * 
 * @author Jennifer Hunt
 *
 */
public class AddIconClickController implements ActionListener{
	
	Lobby lobby;
	UserContext context;
	JTextField iconname;
	JTextField iconurl;
	String name;
	String url;
	
	/**
	 * Constructor for AddIconClickController
	 * @param lobby
	 */
	public AddIconClickController(Lobby lobby, JTextField iconname, JTextField iconurl) {
		this.lobby = lobby;
		context = lobby.getUserContext();
		this.iconname = iconname;
		this.iconurl = iconurl;
	}
	
	/**
	 * 
	 * Button clicked. Message constructed and sent.
	 * 
	 */
	public void actionPerformed(ActionEvent e){
		name = iconname.getText();
		url = iconurl.getText();
		
		String s = "<admin-command version= \"1.0\" id=\"589a39591271844e3fbe32bbb9281ad9\">" +
		"<icon-add icon=\"" + name + "\" uri=\"" + url + "\"/>" + 
		"</admin-command>";

		Document d = Message.construct(s);
		Message m = new Message(d);

		//send to server
		UserContext uc = lobby.getUserContext();
		Client c = uc.getClient();
		c.sendToServer(m);
		
		//refresh the name and url fields
		iconname.setText("");
		iconurl.setText("");
	}

}
